﻿Imports Kimmo

Public Class clsFormDesigner
    Private Shared BE As StringBuilder
    Private Shared Property TableID As Integer = 0
    Private Shared Property TableName As String = ""
    Private Shared Property TargetFolder As String = ""
    Private Shared Property ClassName As String = ""
    Private Shared Property TargetFile As String = ""
    Private Shared Property PrimaryKey As String = ""
    Private Shared Property ProjectName As String = ""
    Private Shared Property Title As String = ""

    Public Shared Function Build(ByVal iTableID As Integer) As String
        Dim clsPB As clsPropertyBuilder
        TableID = iTableID
        If TableID <= 0 Then
            Throw New Exception("Please specify the table id of the table to process first!")
            Exit Function
        End If
        ' get the path of the project
        TargetFolder = clsTables.ProjectPathByID(TableID.ToString)
        TableName = clsTables.TableNameByID(TableID.ToString)
        ProjectName = clsTables.ProjectNameByID(TableID.ToString)
        Title = TableName.Replace("_", " ")
        ClassName = TableName.Replace("_", "").Replace(" ", "")
        TargetFile = TargetFolder & "/frm" & ClassName & ".aspx.designer.vb"
        PrimaryKey = clsColumns.PrimaryKey(TableID)
        ' start processing
        ' process the header
        BE = New StringBuilder
        BE.AppendLine("Option Strict On")
        BE.AppendLine("Option Explicit On")
        BE.AppendLine()
        BE.AppendLine("Partial Public Class frm" & ClassName)
        ' add form controls here
        BE.AppendLine("Protected WithEvents Head1 As Global.System.Web.UI.HtmlControls.HtmlHead")
        BE.AppendLine("Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm")
        BE.AppendLine("Protected WithEvents PageManager1 As Global.FineUI.PageManager")
        BE.AppendLine("Protected WithEvents frmComment As Global.FineUI.Window")
        BE.AppendLine("Protected WithEvents tblComment As Global.FineUI.Toolbar")
        BE.AppendLine("Protected WithEvents ToolbarFill1 As Global.FineUI.ToolbarFill")
        BE.AppendLine("Protected WithEvents txtAddComment As Global.FineUI.TextArea")
        BE.AppendLine("Protected WithEvents RegionPanel1 As Global.FineUI.RegionPanel")
        BE.AppendLine("Protected WithEvents reg" & ClassName & "Center As Global.FineUI.Region")
        BE.AppendLine("Protected WithEvents tbl" & ClassName & "Center As Global.FineUI.Toolbar")
        BE.AppendLine("Protected WithEvents frm" & ClassName & " As Global.FineUI.Form")
        Build_FormControls(BE)
        BE.AppendLine("End Class")

        ' ensure file is deleted first
        WebSite.File_Delete(TargetFile)
        WebSite.File_Update(TargetFile, BE.ToString)
        Return TargetFile
    End Function

    Public Shared Sub Build_FormControls(BE As StringBuilder)
        ' column definitions based on columns defined in the columns table
        Dim xTable As DataTable = SQLCompact.OpenDataTable("select ColumnName,ControlType From Columns Where TableID = " & TableID & " order by [Position];")
        For Each xRow As DataRow In xTable.Rows
            Dim sColumnName As String = xRow("ColumnName").ToString
            Dim sControlType As String = xRow("ControlType").ToString

            BE.AppendLine("Protected WithEvents row" & sColumnName & " As Global.FineUI.FormRow")

            Select Case sControlType
                Case "Image" : BE.AppendLine("Protected WithEvents img" & sColumnName & " As Global.FineUI.Image")
                Case "HyperLink" : BE.AppendLine("Protected WithEvents link" & sColumnName & " As Global.FineUI.HyperLink")
                Case "HtmlEditor" : BE.AppendLine("Protected WithEvents html" & sColumnName & " As Global.FineUI.HtmlEditor")
                Case "CheckBox" : BE.AppendLine("Protected WithEvents chk" & sColumnName & " As Global.FineUI.CheckBox")
                Case "DropDownList" : BE.AppendLine("Protected WithEvents cbo" & sColumnName & " As Global.FineUI.DropDownList")
                Case "TextBox" : BE.AppendLine("Protected WithEvents txt" & sColumnName & " As Global.FineUI.TextBox")
                Case "DatePicker" : BE.AppendLine("Protected WithEvents date" & sColumnName & " As Global.FineUI.DatePicker")
                Case "TextArea" : BE.AppendLine("Protected WithEvents txt" & sColumnName & " As Global.FineUI.TextArea")
                Case "RadioButton" : BE.AppendLine("Protected WithEvents radio" & sColumnName & " As Global.FineUI.RadioButton")
                Case "FileUpload" : BE.AppendLine("Protected WithEvents file" & sColumnName & " As Global.FineUI.FileUpload")
                Case "LinkButton"
                Case "HiddenField" : BE.AppendLine("Protected WithEvents hidden" & sColumnName & " As Global.FineUI.HiddenField")
                Case "NumberBox" : BE.AppendLine("Protected WithEvents number" & sColumnName & " As Global.FineUI.NumberBox")
                Case "TimePicker" : BE.AppendLine("Protected WithEvents time" & sColumnName & " As Global.FineUI.TimePicker")
                Case "TriggerBox" : BE.AppendLine("Protected WithEvents trigger" & sColumnName & " As Global.FineUI.TriggerBox")
                Case "TwinTriggerBox" : BE.AppendLine("Protected WithEvents twintrigger" & sColumnName & " As Global.FineUI.TwinTriggerBox")
                Case "Accordion"
                Case "ContentPanel"
                Case "Form"
                Case "GroupPanel"
                Case "Panel"
                Case "SimpleForm"
                Case "Tree"
                Case "Window"
                Case "Grid"
                Case "RegionPanel"
                Case "TabStrip"
            End Select
        Next
    End Sub

End Class
